<?php
#iearch engine - started @14.04.2004, 22:05 CET
SQLvalidate($_POST['cat']);
SQLvalidate($_GET['cat']);

#check if user can search
$db->query("DELETE FROM `$conf[prefix]search_limit` WHERE time <= ".(time()-$conf['search_limit_time'])) or $db->err(__FILE__, __LINE__);
$db->query("SELECT `ip` FROM `$conf[prefix]search_limit` WHERE ip = '".$_SERVER['REMOTE_ADDR']."'") or $db->err(__FILE__, __LINE__);
if($db->num_rows()!=0) {
	$_GET['error']='search_limit_error';
	include('modules/error.php');
	//redirect('index.php?module=error&amp;error=search_limit_error');
	exit;
}

if($_POST['cat'])
$cat=$_POST['cat'];
else
$cat=$_GET['cat'];

if($cat){
	$cat_ids=get_subcategories($cat);
	$cat_ids[]=$cat;
	$cat_ids_='&& cat IN ('.implode(', ', $cat_ids).')';
			}

			if($_POST['where'])
			$where = $_POST['where'];
			else
			$where = $_GET['where'];

			if($_POST['search'] or $_POST['query'])
			$GLOBALS['query'] = $_POST['search'] ? $_POST['search'] : $_POST['query'];
			else
			$GLOBALS['query'] = $_GET['search'] ? $_GET['search'] : $_GET['query'];

#query longer than 2 signs
			if(strlen(str_replace('*', '', $GLOBALS['query']))<3)
			$GLOBALS['query']='';

#add notice that user searched
			if($GLOBALS['query']) $db->query("INSERT INTO `$conf[prefix]search_limit` (`time`, `ip`) VALUES (".time().", '".$_SERVER['REMOTE_ADDR']."')");

			if(!$where)
	$where='all';

	$GLOBALS['query']=read_text_rest($GLOBALS['query']);
	$query_bak=$GLOBALS['query']; #backup dla wyswietlenia

#####################################
#formulowanie zapytan

#oddzielanie fraz wpisanych w ""
	for($i = 0; $i < preg_match_all('#\\\"(.+)\\\"#iU', $GLOBALS['query'], $match); $i++){
	$GLOBALS['query'] = str_replace($match[0][$i], "", $GLOBALS['query']);
	}

#jezeli jest tylko wyrazenie w " " to wymus przejscie
if(!$GLOBALS['query'] && $tmp[0])
$GLOBALS['query']='#';

#usuwanie niepotrzebnych spacji
$GLOBALS['query'] = eregi_replace("[  ]+", " ", $GLOBALS['query']);
$GLOBALS['query']=trim($GLOBALS['query']);

#zamiana * na % do LIKE
$GLOBALS['query']=str_replace("*", "%", $GLOBALS['query']);

#oddzielanie pozostalych wyraz�w
$src=array();
if(strlen($GLOBALS['query'])>1)
$src=explode(" ",$GLOBALS['query']);

$src_c=count($src);
for($i = 0; $i < count($tmp); $i++)
$src[$i+$src_c] = $tmp[$i];
$src_c=count($src);

#dodanie \ przed znakami specjalnymi dla ereg'a
$src=array_map('quotemeta', $src);
if(!$src && strlen($GLOBALS['query'])>1)
$src[0]=$GLOBALS['query'];

#czyszczenie $t - potrzebne!!
$t=array();

#zmienna przechowujaca tablice do thema
$theme_array=array();

######################################
#wyszukiwanie

#dla kazdego modulu odpal odpowiedni plugin
plugins('search/module/search_modules/');

#kategorie
if(!$_POST['categories'] && $GLOBALS['query']){
	$title_='name_'.$_SESSION['lang_short'];
	$text_='desc_'.$_SESSION['lang_short'];

	$warunki=" ($title_ LIKE '%$src[0]%' || $text_ LIKE '%$src[0]%')";

	for($i = 1; $i<$src_c; $i++)
		$warunki.=" && ($title_ LIKE '%$src[$i]%' || $text_ LIKE '%$src[$i]%')";

	//subcategory of chosen category
	if($cat)
		$warunki.=" && id IN(".implode(", ", $cat_ids).")";

	$category_sort_="ORDER BY $_SESSION[categories_sortby] ".$_SESSION['categories_sort'];
	$db->query("SELECT * FROM $conf[prefix]categories WHERE ($warunki) $category_sort_") or $db->err(__FILE__, __LINE__);

	$i=0;
	while($r = $db->fetch_object()) {
		plugins('search/module/categories/');
		$i++;
	}
}

$GLOBALS['query']=$query_bak;
$description = $lang['search'];

//creating content
$theme_array['theme_path']=$GLOBALS['theme_path'];
//print_r($theme_array);
$content=string_template(read_file('search_main.php').plugins('search/theme/display/', true), $theme_array);
$content=string_template(read_file('middle.php'), array('theme_path'=>$GLOBALS['theme_path'], 'description' => $description, 'content' => $content));
//

//updating site_title
$GLOBALS['lang']['global']['site_title']=$lang['search_for'].' &quot;'.read_text_rest($GLOBALS['query']).'&quot; - '.$GLOBALS['lang']['global']['site_title'];
$GLOBALS['lang']['global']['site_description']=$lang['search_for'].' &quot;'.read_text_rest($GLOBALS['query']).'&quot; - '.$GLOBALS['lang']['global']['site_description'];
//

//generating menus (what, template)
$menu_left=menu('left', 'left');
$menu_right=menu('right', 'right');
$menu_top=menu('top', 'top');
//

//site_generation_time
$GLOBALS['time_end'] = microtime(1);
$site_generation_time=abs(round($GLOBALS['time_end']-$GLOBALS['time_start'], 3));
//

//creating template
$tpl=new tpl;
//header
$tpl->create('header.php', array('title' => $GLOBALS['lang']['global']['site_title'], 'description' => $GLOBALS['lang']['global']['site_description'], 'keywords' => $GLOBALS['lang']['global']['site_keywords'], 'theme_path'=>$GLOBALS['theme_path'], 'menu_top' => $menu_top));
//body
$tpl->create('body.php', array('theme_path' => $GLOBALS['theme_path'], 'menu_top' => $menu_top, 'menu_left' => $menu_left, 'menu_right' => $menu_right, 'content' => $content));
//footer
$tpl->create('footer.php', array('site_generation_time' => string_template($GLOBALS['lang']['global']['site_generation_time'], array('site_generation_time'=>$site_generation_time, 'queries'=>$db->query_count())), 'theme_path'=>$GLOBALS['theme_path'], 'copyright' => $GLOBALS['lang']['global']['site_copyright'], 'queries'=>$db->query_count()));
//parse tpl
$tpl->parse();
//
?>
